package myblog.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import myblog.dao.FavoriteDao;
import myblog.entity.FavoriteEntity;
import myblog.vo.FavoriteVo;

public class FavoriteDaoImpl extends JdbcDaoSupport implements FavoriteDao{

	@Override
	public List<FavoriteEntity> list() {
		String sql = "select * from favorite";

		JdbcTemplate jt = getJdbcTemplate();

		List<FavoriteEntity> favoriteList = jt.query(sql, new FavoriteRowMapper());

		return favoriteList;
	}
	class FavoriteRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			FavoriteEntity entity = new FavoriteEntity();

			entity.setFavorite_id(rs.getString("favorite_id"));
			entity.setLog_id(rs.getString("logid"));
			entity.setUsername(rs.getString("username"));
			return entity;
		}
	}

	@Override
	public void addFavorite(FavoriteVo favoriteVo) {
		JdbcTemplate jt = getJdbcTemplate();

		String sql = "insert into favorite(logid,username) values(?,?)";

		// 构建参数数组
		Object[] args = { favoriteVo.getLog_id(),favoriteVo.getUsername() };

		jt.update(sql, args);
		
	}

	@Override
	public void delFavorite(String favorite_id) {
		JdbcTemplate jt = getJdbcTemplate();

		String sql = "delete from favorite where favorite_id = ?";

		// 构建参数数组
		Object[] args = { favorite_id };

		jt.update(sql, args);

	}

}
